using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using JGSY.CMS.LowCode.Platform.Infrastructure.ServiceCollectionExtensions;

namespace JGSY.CMS.LowCode.Platform.Infrastructure.Configuration.ServiceModules.Modules
{
    /// <summary>
    /// 性能优化服务模块
    /// 包含性能监控、查询优化、智能缓存等高级性能优化服务
    /// </summary>
    public class PerformanceModule : IServiceModule
    {
        public string ModuleName => "性能优化服务";
        public int Priority => 6;
        public bool LoadInFastMode => true; // 快速模式需要基本性能监控
        public ModulePermission RequiredPermission => ModulePermission.PerformanceServices;
        public bool IsCriticalModule => false; // 非关键模块

        public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            // 完整性能优化功能
            ConfigureAdvancedPerformance(services, configuration);
        }

        public void ConfigureMinimalServices(IServiceCollection services, IConfiguration configuration)
        {
            // 精简性能优化功能 - 只配置基本性能监控
            services.AddPerformanceMonitoring(configuration);
        }

        public bool ValidateConfiguration(IConfiguration configuration)
        {
            // 性能模块可以使用默认配置
            return true;
        }

        private void ConfigureAdvancedPerformance(IServiceCollection services, IConfiguration configuration)
        {
            // 高级性能优化服务
            services.AddAdvancedPerformanceServices(configuration);
            services.AddPerformanceMonitoring(configuration);
            services.AddIntelligentCaching(configuration);
            
            // 企业级性能优化系统
            services.AddAdvancedPerformanceOptimization(options =>
            {
                options.EnableCaching = true;
                options.EnablePerformanceMonitoring = true;
                options.EnableQueryOptimization = true;
                
                // 缓存配置
                options.CacheOptions.DefaultExpiration = TimeSpan.FromMinutes(15);
                options.CacheOptions.EnableDistributedCache = true;
                options.CacheOptions.EnableMemoryCache = true;
                
                // 性能监控配置
                options.PerformanceOptions.AlertThresholds.SlowQueryThreshold = TimeSpan.FromSeconds(2);
                options.PerformanceOptions.AlertThresholds.HighMemoryUsage = 0.8;
                options.PerformanceOptions.AlertThresholds.LowCacheHitRate = 0.7;
                options.PerformanceOptions.EnableRealTimeAlerts = true;
                
                // 查询优化配置
                options.QueryOptimizationOptions.EnableSlowQueryLogging = true;
                options.QueryOptimizationOptions.MaxCachedQueries = 1000;
                options.QueryOptimizationOptions.QueryCacheExpiration = TimeSpan.FromMinutes(10);
            });
        }
    }
}
